package com.example.demo.others.git;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.confi.JenkinsOnlineConfi;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/3/17 0017.
 */

@Component
public class AutoPushOnline {


    static String base_target_dir = null;


    @Value("${prd_base_target_dir}")
    public void setBase_target_dir(String base_target_dir) {
        AutoPushOnline.base_target_dir = base_target_dir;
    }

    static Logger logger = LoggerFactory.getLogger(AutoPushOnline.class);

    static String tagPrefix = null;

    @Value("${prd_prefix_tag}")
    public void setTagPrefix(String tagPrefix) {
        AutoPushOnline.tagPrefix = tagPrefix;
    }

    @Value("${prd_prefix_msg}")
    public void setMsgPrefix(String msgPrefix) {
        AutoPushOnline.msgPrefix = msgPrefix;
    }

    static String msgPrefix = null;
    static String pushMessage = ""; //push msg





    public List<JSONObject> run(String name, String msg, String pak) throws  Exception{

        pushMessage = msg;
        Map<String, String> params = new HashMap<>();

        params.put("name", name);
        params.put("pack_dir", pak);

        params.put("base_target_dir", base_target_dir);
        params.put("tagPrefix", tagPrefix);
        params.put("msgPrefix", msgPrefix);
        params.put("pushMessage", pushMessage);


        List<JSONObject> paks = null;
        if (StringUtils.isNotBlank(pak)) {
            List<JSONObject> pags = JenkinsOnlineConfi. packages.stream().filter(p -> pak.contains(p.getString("sour_jar"))).collect(Collectors.toList());
            paks = pags;
        }
        if(paks==null || paks.size()<1)throw  new RuntimeException("no module found");
        return pushUtil.run(params, paks.get(0));
    }

    @Autowired
    AutoPushUtil pushUtil;
}
